const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const cors = require('cors');

const unauthRouter = require('./routes/unauth');
const memberRouter = require('./routes/member');
const { authencation } = require('./middlewares/authencation');
const { freezon } = require('./middlewares/freezon');

const app = express();

// 解决跨域问题
app.use(cors({
    origin: ['http://localhost:3000'],
    methods: ['GET', 'POST'],
    allowedHeaders: ['Content-Type', 'Authorization'],
    exposedHeaders: ['Authorization'],
    credentials: true,
    // 1小时内不需要预检请求
    maxAge: 3600
}));

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/loader', express.static(path.join(__dirname, 'storage/public')));

app.use('/direct', unauthRouter);

app.use(authencation);

app.use('/member', memberRouter);

/* auth token. */
app.post('/direct/token-validate', async function(req, res, next) {
    res.json({ok: 1});
});

app.use(freezon);

module.exports = app;
